#ifndef ABSTRACTMODEL_H
#define ABSTRACTMODEL_H

#include <QList>
#include <QListIterator>
#include "Observable_I.h"
#include "Observer_I.h"
#include "Myobject.h"

using namespace mvc;

template<typename T>class AbstractModel : public Observable_I<T>, public MyObject
    {
        typedef QSharedPointer<Observer_I<T> > ObserverPtr;
    protected:
        T m_data;
    private:
        QList<ObserverPtr> m_listObserver;
    public:
        AbstractModel();
        virtual ~AbstractModel();

        virtual void reset() = 0;
        virtual T getData() = 0;
        virtual void setData(T data) = 0;

        void addObserver(ObserverPtr &);
        void removeObserver();
        void notifyObserver(T before, T after);
    };

template<typename T> AbstractModel<T>::AbstractModel():MyObject()
    {
    print()<<"AbstractModel construction"<<std::endl;
    }

template<typename T> AbstractModel<T>::~AbstractModel()
    {
    print()<<"AbstractModel destruction"<<std::endl;
    print()<<m_listObserver.size()<<std::endl;
    }

template<typename T> void AbstractModel<T>::removeObserver()
    {
    m_listObserver.clear();
    }

template<typename T> void AbstractModel<T>::notifyObserver(T before, T after)
    {
    QListIterator<ObserverPtr > i(m_listObserver);
    while(i.hasNext())
        {
        i.next().data()->update(before, after);
        }
    }

template<typename T> void AbstractModel<T>::addObserver(ObserverPtr & obs)
    {
    m_listObserver.append(obs);
    }

#endif // ABSTRACTMODEL_H
